/*
 * This file is part of the GUI_SDK.
 *
 * Copyright (c) 2019-2019 Link-Card.Co.Ltd. All rights reserved.
 *
 * Authors:
 *    JiangYong <1556631285@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Function:  log to file
 * Created on: 2019-10-05
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <time.h>
#include <pthread.h>
#include <sys/syscall.h>

//此处生成日志文件路径不能修改
#define FILE_LOG_PATH "/mnt/mmc/app.log"

// #define DEBUG_LOG

#ifdef  DEBUG_LOG
#define m_DEBUG  FILE_DBG
#define m_MSG    FILE_MSG
#define m_ERROR  FILE_ERR
#else
#define m_DEBUG(format, args...)
#define m_MSG(format, args...) 
#define m_ERROR(format, args...)  printf(format, ## args);fflush(stdout);
#endif


#define FILE_DBG(fmt,args...) do{ \
		struct tm tm; \
		time_t t; \
		time( &t ); \
		localtime_r( &t, &tm ); \
		FILE *out_tmp = fopen(FILE_LOG_PATH, "a+w"); \
		if (out_tmp) { \
			fprintf(out_tmp, "Mon:%2d Day:%2d %02d:%02d:%02d,file:%s line:%d, function:%s  ",tm.tm_mon, tm.tm_mday,\
	                  tm.tm_hour, tm.tm_min, tm.tm_sec, __FILE__,__LINE__,__FUNCTION__);	\
			fprintf(out_tmp, (char*)fmt, ##args); \
			fprintf(out_tmp, "\n"); \
			fflush(out_tmp);		\
			fclose(out_tmp);		\
		} \
}while(0)


#define FILE_MSG(fmt,args...) do{ \
	struct tm tm; \
	time_t t; \
	time( &t ); \
	localtime_r( &t, &tm ); \
	FILE *out_tmp = fopen(FILE_LOG_PATH, "a+w"); \
	if (out_tmp) { \
		fprintf(out_tmp, "Mon:%2d Day:%2d %02d:%02d:%02d,file:%s line:%d, function:%s  ",tm.tm_mon, tm.tm_mday,\
				  tm.tm_hour, tm.tm_min, tm.tm_sec, __FILE__,__LINE__,__FUNCTION__);	\
		fprintf(out_tmp, (char*)fmt, ##args); \
		fprintf(out_tmp, "\n"); \
		fflush(out_tmp);		\
		fclose(out_tmp);		\
		} \
}while(0)


#define FILE_ERR(fmt,args...) do{ \
	struct tm tm; \
	time_t t; \
	time( &t ); \
	localtime_r( &t, &tm ); \
	FILE *out_tmp = fopen(FILE_LOG_PATH, "a+w"); \
	if (out_tmp) { \
		fprintf(out_tmp, "Mon:%2d Day:%2d %02d:%02d:%02d,file:%s line:%d, function:%s  ",tm.tm_mon, tm.tm_mday,\
				  tm.tm_hour, tm.tm_min, tm.tm_sec, __FILE__,__LINE__,__FUNCTION__);	\
		fprintf(out_tmp, (char*)fmt, ##args); \
		fprintf(out_tmp, "\n"); \
		fflush(out_tmp);		\
		fclose(out_tmp);		\
		} \
}while(0)
